home *** CD-ROM | disk | FTP | other *** search
- /*
- Filename Carpet.c
- Copyright © 1993 David Hart. All rights reserved.
- Author David Hart
- Description Carpet module for After Dark
-
- Version Date Comments
- 1.00 01/03/93 Original Version
- 1.01 07/03/93 Remove blackColor
- 1.02 08/03/93 Shape selection by popup menu
- 2.00 17/03/93 Updated for After Dark version 2.0
- 2.01 22/03/93 To draw on every monitor
- */
-
- #include <QuickDraw.h>
- #include <Memory.h>
- #include <OSUtils.h>
- #include "GraphicsModule_Types.h"
-
- /* Function prototypes */
- int Rand(int range);
- void PaintShapes(int xmid, int ymid, int x, int y, int dx, int dy, int type);
- void PaintShape(Rect *rect, int dx, int dy, int type);
-
- long Colors[] = {
- /* blackColor, */
- whiteColor,
- redColor,
- greenColor,
- blueColor,
- cyanColor,
- magentaColor,
- yellowColor };
-
- /* Specific graphics module data structures */
- typedef struct MyStorage
- {
- int dummy;
- } MyStorage, *MyStoragePtr, **MyStorageHandle;
-
- /*
- DoInitialize is the first function called from After Dark.
- Memory for the structure is allocated and the variables are initialized.
- The allocated memory is assigned to "storage"
- and the function returns "noErr" if there are no problems.
- */
- OSErr DoInitialize(Handle *storage, RgnHandle blankRgn, GMParamBlockPtr params)
- {
- MyStorageHandle myStorage;
-
- /* allocate handle to my storage */
- myStorage = (MyStorageHandle)NewHandle(sizeof(MyStorage));
-
- if (!myStorage)
- return MemError();
-
- MoveHHi(myStorage);
- HLock(myStorage); /* Lock it down */
-
- *storage = (Handle)myStorage; /* Assign myStorage to passed handle */
-
- HUnlock(myStorage);
- return noErr;
- }
-
- /*
- DoBlank is the next function called.
- It is used to simply blank the screen.
- */
- OSErr DoBlank(Handle storage, RgnHandle blankRgn, GMParamBlockPtr params)
- {
- FillRgn(blankRgn, params->qdGlobalsCopy->qdBlack);
- return noErr;
- }
-
- /*
- DoDrawFrame does almost all of the work.
- */
- OSErr DoDrawFrame(Handle storage, RgnHandle blankRgn, GMParamBlockPtr params)
- {
- OSErr err = noErr;
- MyStoragePtr myStorage; /* to hold dereferenced storage handle */
- int x;
- int y;
- int dx;
- int dy;
- int xmid;
- int ymid;
- int width;
- int height;
- int size;
- int color;
- int monitor;
- Rect rect;
-
- /* Lock our storage down so we can dereference it once for faster access */
- MoveHHi(storage);
- HLock(storage);
- myStorage = (MyStoragePtr)*storage;
-
- /* For each monitor */
- for (monitor = 0; monitor < params->monitors->monitorCount; monitor++)
- {
- rect = params->monitors->monitorList[monitor].bounds;
- xmid = (rect.left + rect.right) / 2;
- ymid = (rect.top + rect.bottom) / 2;
- width = rect.right - rect.left;
- height = rect.bottom - rect.top;
- size = width > height ? width : height;
-
- /* Random x, y */
- x = Rand(size/2)+1;
- y = Rand(x)+1;
-
- /* Random dx, dy */
- dx = params->controlValues[0];
- dy = params->controlValues[1];
-
- dx = Rand(dx < 4 ? 4 : dx) + 1;
- dy = Rand(dy < 2 ? 2 : dy) + 1;
-
- /* Random color */
- color = Rand(7);
- ForeColor(Colors[color]);
-
- /* Paint Shapes */
- PaintShapes(xmid, ymid, x, y, dx, dy, params->controlValues[2]);
- }
-
- HUnlock(storage);
- return noErr;
- }
-
- /*
- DoClose is called when the user quits the screen saver.
- The memory is deallocated and the function returns "MemError"
- which will tell After Dark if there was a problem
- */
- OSErr DoClose(Handle storage, RgnHandle blankRgn, GMParamBlockPtr params)
- {
- MyStorageHandle myStorage = (MyStorageHandle)storage;
-
- /* Deallocate our storage */
- if (myStorage)
- {
- MoveHHi(myStorage);
- HLock(myStorage);
- DisposHandle(storage);
- }
-
- /* check for memory errors */
- return MemError();
- }
-
- /*
- DoSetUp is called if the user clicks on a button in the Control Panel.
- */
- OSErr DoSetUp(RgnHandle blankRgn, short message, GMParamBlockPtr params)
- {
- return noErr;
- }
-
- /*
- Rand returns value (0..range-1)
- */
- int Rand(int range)
- {
- long result = Random();
-
- if (result < 0)
- result =- result;
-
- return ((result * range) / 32768);
- }
-
- /*
- PaintShapes paints eight symmetrically placed shapes
- */
- void PaintShapes(int xmid, int ymid, int x, int y, int dx, int dy, int type)
- {
- Rect rect;
-
- /* x, y */
- SetRect(&rect, xmid+(x-dx), ymid+(y-dy), xmid+(x+dx), ymid+(y+dy));
- PaintShape(&rect, dx, dy, type);
-
- /* -x, y*/
- SetRect(&rect, xmid-(x+dx), ymid+(y-dy), xmid-(x-dx), ymid+(y+dy));
- PaintShape(&rect, dx, dy, type);
-
- /* x, -y */
- SetRect(&rect, xmid+(x-dx), ymid-(y+dy), xmid+(x+dx), ymid-(y-dy));
- PaintShape(&rect, dx, dy, type);
-
- /* -x, -y */
- SetRect(&rect, xmid-(x+dx), ymid-(y+dy), xmid-(x-dx), ymid-(y-dy));
- PaintShape(&rect, dx, dy, type);
-
- /* y, x*/
- SetRect(&rect, xmid+(y-dy), ymid+(x-dx), xmid+(y+dy), ymid+(x+dx));
- PaintShape(&rect, dx, dy, type);
-
- /* -y, x*/
- SetRect(&rect, xmid-(y+dy), ymid+(x-dx), xmid-(y-dy), ymid+(x+dx));
- PaintShape(&rect, dx, dy, type);
-
- /* y, -x*/
- SetRect(&rect, xmid+(y-dy), ymid-(x+dx), xmid+(y+dy), ymid-(x-dx));
- PaintShape(&rect, dx, dy, type);
-
- /* -y, -x*/
- SetRect(&rect, xmid-(y+dy), ymid-(x+dx), xmid-(y-dy), ymid-(x-dx));
- PaintShape(&rect, dx, dy, type);
- }
-
- /*
- PaintShape paints single shape according to type
- */
- void PaintShape(Rect *rect, int dx, int dy, int type)
- {
- switch (type)
- {
- case 1: /* Rectangle */
- PaintRect(rect);
- break;
- case 2: /* Rounded */
- PaintRoundRect(rect, dx, dy);
- break;
- case 3: /* Rectangle */
- PaintOval(rect);
- break;
- }
- }
-